<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>重庆鸡公煲叫号器</title>
    <style>
        body {
            font-family: '微软雅黑', Arial, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            color: #333;
        }

        .container {
            background-color: #ffffff;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
            padding: 40px 30px;
            width: 90%;
            max-width: 500px;
            text-align: center;
            box-sizing: border-box;
        }

        h1 {
            margin-top: 0;
            margin-bottom: 25px;
            font-size: 28px;
            color: #2c3e50;
            text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
        }

        .input-group {
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 30px;
        }

        label {
            font-size: 18px;
            margin-right: 15px;
            white-space: nowrap;
        }

        input[type="text"] {
            padding: 12px 15px;
            font-size: 16px;
            border: 2px solid #ddd;
            border-radius: 8px;
            width: 200px;
            box-sizing: border-box;
            transition: border-color 0.3s ease;
        }

        input[type="text"]:focus {
            border-color: #3498db;
            outline: none;
            box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
        }

        button {
            background: linear-gradient(to right, #27ae60, #2ecc71);
            color: white;
            border: none;
            padding: 15px 30px;
            font-size: 18px;
            font-weight: bold;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(39, 174, 96, 0.3);
            width: 100%;
            max-width: 300px;
            margin: 0 auto 25px auto;
            display: block;
        }

        button:hover {
            background: linear-gradient(to right, #219653, #27ae60);
            box-shadow: 0 6px 20px rgba(39, 174, 96, 0.4);
            transform: translateY(-2px);
        }

        button:active {
            transform: translateY(1px);
        }

        button:disabled {
            background: linear-gradient(to right, #bdc3c7, #95a5a6);
            cursor: not-allowed;
            box-shadow: none;
            transform: none;
        }

        #status {
            font-size: 16px;
            min-height: 24px;
        }

        .status-idle {
            color: #7f8c8d;
        }

        .status-calling {
            color: #2980b9;
            font-weight: bold;
        }

        .status-done {
            color: #27ae60;
            font-weight: bold;
        }
    </style>
</head>
<body>

<div class="container">
    <h1>重庆鸡公煲叫号器</h1>
    <div class="input-group">
        <label for="customerName">顾客姓名:</label>
        <input type="text" id="customerName" placeholder="请输入姓名" value="刘金燕">
    </div>
    <button id="callButton">叫号</button>
    <div id="status" class="status-idle">准备就绪</div>
</div>

<script>
    let isCalling = false;
    let utteranceQueue = [];
    let isSpeaking = false;

    const nameInput = document.getElementById('customerName');
    const callButton = document.getElementById('callButton');
    const statusDiv = document.getElementById('status');

    // 检查浏览器是否支持 Web Speech API
    const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
    const SpeechSynthesis = window.speechSynthesis;

    if (!SpeechSynthesis) {
        statusDiv.textContent = '抱歉，您的浏览器不支持语音合成功能。';
        statusDiv.className = 'status-idle';
        callButton.disabled = true;
    } else {
        statusDiv.textContent = '准备就绪';
        statusDiv.className = 'status-idle';
    }

    function speak(text) {
        return new Promise((resolve, reject) => {
            if (!SpeechSynthesis) {
                reject(new Error('SpeechSynthesis not supported'));
                return;
            }

            const utterance = new SpeechSynthesisUtterance(text);
            utterance.lang = 'zh-CN';
            utterance.rate = 1.0; 
            utterance.pitch = 1.0;
            utterance.volume = 1.0;

            utterance.onend = () => {
                console.log(`Finished speaking: ${text}`);
                resolve();
            };

            utterance.onerror = (event) => {
                console.error('SpeechSynthesis Error:', event);
                reject(event);
            };

            SpeechSynthesis.speak(utterance);
        });
    }

    async function callName(name) {
        const message = `重庆鸡公煲请 ${name} 同学取餐`;
        isCalling = true;
        updateUI(true, `正在呼叫: ${name}`, 'status-calling');

        try {
            // 播报三遍
            for (let i = 0; i < 3; i++) {
                if (!isCalling) break; // 如果用户中断，则停止
                console.log(`Speaking iteration ${i + 1}`);
                await speak(message);
            }
        } catch (error) {
            console.error("播报出错:", error);
            updateUI(false, '播报出错!', 'status-idle');
        } finally {
            if (isCalling) { // 仅在未被中断时更新为完成状态
                 updateUI(false, '播报完成', 'status-done');
            }
            isCalling = false;
        }
    }

    function updateUI(isInProgress, statusText, statusClass) {
         if (isInProgress) {
            callButton.textContent = '播报中...';
            callButton.disabled = true;
        } else {
            callButton.textContent = '叫号';
            callButton.disabled = false;
        }
        statusDiv.textContent = statusText;
        statusDiv.className = statusClass;
    }

    callButton.addEventListener('click', () => {
        const name = nameInput.value.trim();
        if (!name) {
            statusDiv.textContent = '请输入顾客姓名!';
            statusDiv.className = 'status-idle';
            nameInput.focus();
            return;
        }
        callName(name);
    });

    // 绑定回车键
    nameInput.addEventListener('keypress', (e) => {
        if (e.key === 'Enter' && !callButton.disabled) {
            callButton.click();
        }
    });

    // 可选：点击窗口任何地方使输入框失去焦点，避免虚拟键盘问题
    document.addEventListener('click', (e) => {
        if (e.target !== nameInput) {
            nameInput.blur();
        }
    });

</script>

</body>
</html>
